package cn.lutu.domain.repository.impl;

import cn.lutu.dao.mapper.SpotMapper;
import cn.lutu.domain.repository.AdminRepository;
import cn.lutu.dao.mapper.AdminMapper;
import cn.lutu.protocol.dto.AdminSelectUserDTO;
import cn.lutu.protocol.entity.Authority;
import cn.lutu.protocol.entity.Spot;
import cn.lutu.protocol.entity.UserAndRole;
import cn.lutu.protocol.vo.RoleVO;
import cn.lutu.protocol.vo.adminVO.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class AdminRepositoryImpl implements AdminRepository {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    SpotMapper spotMapper;


    @Override
    public List<AdminUserListVO> userList() {
        log.info("开始处理AdminRepositoryUserList请求");
        return adminMapper.selectAdminUserList();
    }

    @Override
    public List<AdminRoleListVO> roleList() {
        log.info("开始处理AdminRepositoryRoleList请求");
        return adminMapper.selectRoleList();
    }

    @Override
    public List<AdminSpotVO> spotList() {
        log.info("开始处理AdminRepositorySpotList请求");
        return adminMapper.selectSpot();
    }

    @Override
    public List<AuthorityUpdateVO> authorityList() {
        log.info("开始处理AdminRepositoryRoleList请求");
        return adminMapper.selectAuthority();
    }

    @Override
    public List<AdminUserListVO> userListBy(AdminSelectUserDTO adminSelectUserDTO) {
        log.info("Repository层开始处理管理员根据条件查询用户信息，参数为：{}",adminSelectUserDTO);
        return adminMapper.userListBy(adminSelectUserDTO);
    }

    @Override
    public UserDetailVO getUserInfoByUsername(String userName) {
        log.debug("开始执行【根据用户名查询用户的登录信息】，参数：{}", userName);
        return adminMapper.getUserInfoByUsername(userName);
    }

    @Override
    public RoleVO getRoleInfoByName(String name) {
        log.debug("开始执行【根据角色名查询角色信息】，参数：{}", name);
        return adminMapper.getRoleInfoName(name);
    }

    @Override
    public AuthorityUpdateVO getAuthorityInfoByName(String name) {
        log.debug("开始执行【根据角色名查询权限信息】，参数：{}", name);
        return adminMapper.getAuthorityInName(name);
    }

    @Override
    public int deleteById(Long id) {
        return adminMapper.deleteById(id);
    }

    @Override
    public int deleteAuthorityById(Long id) {
        log.debug("开始执行【根据删除角色信息】，参数：{}", id);
        return adminMapper.deleteAuthorityById(id);
    }

    @Override
    public UserAndRole selectById(Long id) {
        log.info("开始执行根据ID查询角色的功能，名称为：{}",id);
        UserAndRole userAndRole = adminMapper.selectById(id);
        return userAndRole;
    }

    @Override
    public AdminSpotVO getSpotById(Long id) {
        log.info("开始执行根据ID查询景点的功能，名称为：{}",id);
        AdminSpotVO adminSpotVO = adminMapper.getSpotById(id);
        return adminSpotVO;
    }

    @Override
    public AuthorityUpdateVO getAuthorityById(Long id) {
        log.info("开始执行根据ID查询权限的功能，名称为：{}",id);
        AuthorityUpdateVO authority = adminMapper.getAuthorityInfo(id);
        return authority;
    }


    @Override
    public RoleUpdateInfoVO getSelectById(Long id) {
        log.debug("开始执行【根据ID查询标签】，参数：{}", id);
        return adminMapper.getUpdateById(id);
    }

    @Override
    public int updateById(UserAndRole userAndRole) {
        log.debug("开始执行【根据ID修改数据】，参数：{}", userAndRole);
        return adminMapper.updateById(userAndRole);
    }

    @Override
    public int updateAuthorityById(Authority authority) {
        log.debug("开始执行【根据ID修改权限数据】，参数：{}", authority);
        return adminMapper.updateAuthority(authority);
    }

    @Override
    public int countByNameAndNotId(Long id, String name) {
        log.debug("开始执行【统计匹配名称查不匹配ID的标签数量】，ID：{}，名称：{}", id, name);
        QueryWrapper<UserAndRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).ne("id", id);
        return adminMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(UserAndRole userAndRole) {
        log.debug("开始执行【根据新增角色数据】，参数：{}", userAndRole);
        return adminMapper.insert(userAndRole);
    }

    @Override
    public int updateSpotById(Spot spot) {
        log.debug("开始执行【根据修改景点数据】，参数：{}", spot);
        return spotMapper.updateById(spot);
    }


}
